package com.gitee.wsl.mathematics.geometry.d2.rect

import com.gitee.wsl.io.dataproxy.asProxy
import com.gitee.wsl.mathematics.geometry.d2.AbstractCloseShape
import com.gitee.wsl.mathematics.geometry.d2.base.YPolarity
import com.gitee.wsl.mathematics.geometry.d2.circle.Circle
import com.gitee.wsl.mathematics.geometry.d2.contour.ShapeContour
import com.gitee.wsl.mathematics.geometry.d2.line.Line
import com.gitee.wsl.mathematics.geometry.d2.triangle.Triangle
import com.gitee.wsl.mathematics.vector.vec2.Vec2f

class Rectangle(a: Vec2f, b: Vec2f,): AbstractCloseShape(proxy = arrayOf(a,b).asProxy()),
    RectangleShape2d<Float, Vec2f,Rectangle> {
    constructor(x: Float,  y: Float,  x1: Float,  y1: Float):this(Vec2f(x,y),Vec2f(x1,y1))
    constructor(x: Number,  y: Number,  x1: Number,  y1: Number):this(Vec2f(x,y),Vec2f(x1,y1))
    constructor(startP:Vec2f,width:Number,height:Number):this(startP,Vec2f(startP.x + width.toFloat(),startP.y+height.toFloat()))

    constructor(circle: Circle):this(
        circle.center.x - circle.radius,
        circle.center.y - circle.radius,
        circle.center.x + circle.radius,
        circle.center.y + circle.radius)

    constructor(triangle: Triangle):this(Circle(triangle))

    override fun createPoint2(x: Float, y: Float) = Vec2f(x,y)

    override fun create(a: Vec2f, b: Vec2f, ):Rectangle = Rectangle(a,b)

    override fun createLine(a: Vec2f, b: Vec2f) = Line(a,b)

    /** Returns [ShapeContour] representation of the [Rectangle]. */
    override val contour: ShapeContour
        get() {
            return if (corner == Vec2f.INFINITY || corner.x != corner.x || corner.y != corner.y || width != width || height != height) {
                ShapeContour.EMPTY
            } else {
                ShapeContour.fromPoints(
                    listOf(
                        corner, corner + Vec2f(width, 0.0),
                        corner + Vec2f(width, height),
                        corner + Vec2f(0.0, height)
                    ), true, YPolarity.CW_NEGATIVE_Y
                )
            }
        }

    companion object{
        /** A zero-length [Rectangle]. */
        val EMPTY = Rectangle(0.0, 0.0, 0.0, 0.0)

        /** Creates a new [Rectangle] by specifying the [center] position with dimensions [width] and [height]. */
        fun fromCenter(center: Vec2f, width: Float, height: Float = width) =
            fromAnchor(Vec2f(0.5, 0.5), center, width, height)


        /** Creates a new [Rectangle] by specifying the [anchorUV], [anchor] positions with dimensions [width] and [height]. */
        fun fromAnchor(anchorUV: Vec2f, anchor: Vec2f, width: Float, height: Float = width) =
            Rectangle(Vec2f(anchor.x - width * anchorUV.x, anchor.y - height * anchorUV.y), width, height)

    }

}

